package com.dididoctor.patient.Activity.Usercentre.Record;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dididoctor.patient.Activity.Usercentre.Record.Cases.CasesFragment;
import com.dididoctor.patient.Activity.Usercentre.Record.Health.HealthFragment;
import com.dididoctor.patient.MV.EduActivity;
import com.dididoctor.patient.R;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： RecordActivity
 * @包名称： com.dididoctor.patient.Activity.Usercentre.Health
 * @创建人： zhanjunjie
 * @创建时间：2016/4/14 19:45
 * @修改人：
 * @修改时间：2016/4/14 19:45
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class RecordActivity extends EduActivity {
    private Fragment[] fgs;
    private int curPos = 0;
    private TextView[] textViews;
    private final int textViewIds[] = new int[]{
            R.id.tv_title1, R.id.tv_title2
    };
    private final int layoutIds[] = new int[]{
            R.id.layout1, R.id.layout2
    };
    private ViewPager viewPager;
    private TextView tv_title, tv_finish;
    private CasesFragment schoolQuesFragment;
    private HealthFragment forumModuleFragment;


    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_record);
    }


    @Override
    public void init() {
        initTopbar();

        textViews = new TextView[2];
        LinearLayout[] layouts = new LinearLayout[2];
        for (int i = 0; i < 2; i++) {
            textViews[i] = (TextView) findViewById(textViewIds[i]);
            layouts[i] = (LinearLayout) findViewById(layoutIds[i]);
            layouts[i].setOnClickListener(this);
        }

        viewPager = (ViewPager) findViewById(R.id.frg_layout);
        forumModuleFragment = new HealthFragment();
        schoolQuesFragment = new CasesFragment();

        fgs = new Fragment[]{forumModuleFragment, schoolQuesFragment};

        viewPager.setAdapter(new ForumTabAdapter(getSupportFragmentManager()));
        viewPager.setOffscreenPageLimit(2);
        viewPager.setCurrentItem(0);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int i) {
                select(i);
                if (i == 0) {
                    tv_finish.setVisibility(View.VISIBLE);
                } else {
                    tv_finish.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }

    private void select(int i) {
        if (curPos != i) {
            textViews[curPos].setTextColor(Color.parseColor("#fcb314"));
            textViews[i].setTextColor(Color.parseColor("#f3f3f3"));
            textViews[i].setBackgroundColor(Color.parseColor("#fcb314"));
            textViews[curPos].setBackgroundColor(Color.parseColor("#f3f3f3"));
            curPos = i;
        }
    }

    private void initTopbar() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("健康档案");
        tv_finish = (TextView) findViewById(R.id.tv_finish);
        tv_finish.setText("保存");
        tv_finish.setVisibility(View.VISIBLE);
        tv_finish.setOnClickListener(this);
    }

    @Override
    public void onClickEvent(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_finish:
                Intent intent = new Intent("SubmitReceiver");
                sendBroadcast(intent);
                break;
        }

        if (id == layoutIds[0]) {
            select(0);
            viewPager.setCurrentItem(0);
            tv_finish.setVisibility(View.VISIBLE);
        } else if (id == layoutIds[1]) {
            viewPager.setCurrentItem(1);
            tv_finish.setVisibility(View.GONE);
            select(1);
        }
    }


    class ForumTabAdapter extends FragmentPagerAdapter {

        public ForumTabAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fgs[i];
        }

        @Override
        public int getCount() {
            return fgs.length;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1003 && resultCode == RESULT_OK) {
            schoolQuesFragment.getcases();
        }
    }
}
